package com.stylefeng.guns;



import org.apache.commons.io.FileUtils;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.core.FileURIResolver;
import org.apache.poi.xwpf.converter.core.IImageExtractor;
import org.apache.poi.xwpf.converter.core.IURIResolver;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * word 转换成html
 */
public class DocxToHtml {

    public static void main(String[] args) {
        try {
            String filepath = "D:\\docx\\";
            String fileName = "DDD.docx";
            String htmlName = "滕王阁序2007.html";
            final String file = filepath + fileName;
            File f = new File(file);
            if (!f.exists()) {
                System.out.println("Sorry File does not Exists!");
            } else {
                if (f.getName().endsWith(".docx") || f.getName().endsWith(".DOCX")) {

                    // 1) 加载word文档生成 XWPFDocument对象
                    InputStream in = new FileInputStream(f);
                    XWPFDocument document = new XWPFDocument(in);
                    document.getAllPictures();
                    // 2) 解析 XHTML配置 (这里设置IURIResolver来设置图片存放的目录)
                    File imageFolderFile = new File(filepath);
                    XHTMLOptions options = XHTMLOptions.create();
                    options.URIResolver(new FileURIResolver(imageFolderFile));
                    options.setExtractor(new FileImageExtractor(imageFolderFile));
                    options.setIgnoreStylesIfUnused(false);
                    options.setFragment(true);

                    // 3) 将 XWPFDocument转换成XHTML
/*                    OutputStream out = new FileOutputStream(new File(filepath + htmlName));
                    XHTMLConverter.getInstance().convert(document, out, options);*/

                    //也可以使用字符数组流获取解析的内容
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    XHTMLConverter.getInstance().convert(document, baos, options);
                    String content = baos.toString();
                    System.out.println(content);
                    baos.close();
                    FileUtils.writeStringToFile(new File(filepath, "test.html"), new String(baos.toByteArray()), "utf-8");
                } else {
                    System.out.println("Enter only MS Office 2007+ files");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public static void test() {
/*        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String ymd = sdf.format(new Date());
        String savePath = ymd + "/";
        String saveUrl = ymd + "/";
        XHTMLOptions options = XHTMLOptions.create();
        // Extract image
        //  File imageFolder = new File( FileTools.getInstance().getRoot() + savePath);
        IImageExtractor extractor=   new IImageExtractor() {
            private String savePath="";
            public IImageExtractor setSavePath(String savePath) {
                this.savePath = savePath;
                return this;
            }
            @Override
            public void extract(String arg0, byte[] arg1) throws IOException {
                byte[] bytev =arg1;
                String fileName = arg0.substring(arg0.lastIndexOf("/")+1);
                FileTools.getInstance().saveFile(savePath, fileName,bytev);
            }
        }.setSavePath(savePath) ;
        options.setExtractor(extractor );
        options.URIResolver( new  IURIResolver() {
            private String savePath="";
            public IURIResolver init(String savePath) {
                this.savePath = savePath;
                return this;
            }
            @Override
            public String resolve(String arg0) {
                String fileName = arg0.substring(arg0.lastIndexOf("/")+1);
                return savePath+ fileName + ".html";
            }
        } .init(saveUrl));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        XHTMLConverter.getInstance().convert( doc, out, options );
        String str = out.toString();*/



/*        //          writeFile(new String("<html><head>  <meta http-equiv=\"content-type\" content=\"text/html\" charset=\"utf-8\"/></head>对不起，.docx格式的word文档，暂时不能生成预览</html>".getBytes("utf-8")), outPutFilePath+newFileName);

        //step 1 : load DOCX into XWPFDocument
        InputStream inputStream = new FileInputStream(new File(fileName));
        XWPFDocument document = new XWPFDocument(inputStream);

        //step 2 : prepare XHTML options
        final String imageUrl = "";

        XHTMLOptions options = XHTMLOptions.create();
        options.setExtractor(new FileImageExtractor(new File(outPutFilePath + imageUrl)));
        options.setIgnoreStylesIfUnused(false);
        options.setFragment(true);
        options.URIResolver(new IURIResolver() {
            //              @Override 重写的方法，加上这个报错，你看看是啥问题
            public String resolve(String uri) {
                return imageUrl + uri;
            }
        });

        //step 3 : convert XWPFDocument to XHTML
        XHTMLConverter.getInstance().convert(document, out, options);*/
    }
}

